import { Component, OnInit, Output, EventEmitter, ElementRef } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from 'app/public/serve/hints/hints.service';

import { ipRule } from '../ip-rule.data';
import { ipRuleUrl } from '../ip-rule.url';

declare var $: any;

@Component({
  selector: 'ip-rule-newly-added',
  templateUrl: './ip-rule-newly-added.component.html',
  styleUrls: ['./ip-rule-newly-added.component.css']
})

export class IpRuleNewlyAddedComponent implements OnInit {
  
  @Output() onSubmit = new EventEmitter<void>();

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService
  ) {}

  ngOnInit() {
  }

  /**
   * 新增
   * @param info 
   */ 
  _onSubmit(info: object) {

    info['userId']        = ipRule.user.userId;
    info['institutionId'] = ipRule.tree.institutionId;

    this.request
        .postRequest(ipRuleUrl.addIprules, info)
        .then(res=>{
          if (res.result_code=='0000') {
            this.onSubmit.emit();
          }
          this.hintsService._operationHints(res.message, res.result_code);
          this._init();
          return
        })
        .catch(err=> {
          this.hintsService._operationHints('error', 'error');
          this._init();
          return
        })
  }

  /**
   * 初始化弹窗
   */
  private _init() {
    this.initService._init();
    $('#newlyAdded').modal('hide');
  }

}
